﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;


namespace DTcms.Web.m
{
    /// <summary>
    /// bendiquan_company_list 的摘要说明
    /// </summary>
    public class bendiquan_company_list : IHttpHandler, IRequiresSessionState
    {
        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;

        protected int quanzi_id = 0;

        //修改本地圈 全部企业查找方式，添加省市和类别  2019-01-23
        protected string province = string.Empty;
        protected string category = string.Empty;
        protected int province_id = 0;
        protected int category_id = 0;
        public void ProcessRequest(HttpContext context)
        {

            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);

            this.quanzi_id = DTRequest.GetQueryInt("quanzi_id");

            province = Utils.ToHtml(DTRequest.GetQueryString("province"));
            category = Utils.ToHtml(DTRequest.GetQueryString("category"));

            if (quanzi_id == 0) { quanzi_id = 1; }
            //category = Utils.ToHtml(DTRequest.GetQueryString("category"));
            ///添加产品分类筛选条件  Demon 2018-12-02
            int category_id = DTRequest.GetQueryInt("category_id", 0);
            if (!string.IsNullOrEmpty(province))
            {
                if (province != "不限")
                {
                    province_id = new BLL.province().GetID(province);
                }
            }
            if (!string.IsNullOrEmpty(category))
            {
                if (category != "不限")
                {
                    category_id = new BLL.company_category().GetID(category);
                }
            }

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.company bll = new BLL.company();

            StringBuilder strWhere = new StringBuilder();
            strWhere.Append("id>0");
           
            strWhere.Append(" and quanzi_id =" + quanzi_id);
            ///添加产品分类筛选条件  Demon 2018-11-30
            if (category_id > 0)
            {
                strWhere.Append(" and category_id=" + category_id);
            }

            if (province_id > 0)
            {
                strWhere.Append(" and province_id =" + province_id);
            }
            if (category_id > 0)
            {
                strWhere.Append(" and category_id =" + category_id);
            }
            DataSet ds = bll.GetList(this.pageSize, this.page, strWhere.ToString(), "sort_id desc,group_id desc,id asc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString() + ",");
            //如果记录存在
            if (ds.Tables[0].Rows.Count > 0)
            {
                strTxt.Append("\"company_list\":[");
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    strTxt.Append("{");
                    strTxt.Append("\"id\":" + dr["id"]);
                    strTxt.Append(",\"title\":\"" + dr["title"] + "\"");
                    string imglogo = dr["logo"].ToString();
                    if (string.IsNullOrEmpty(imglogo))
                    {
                        strTxt.Append(",\"img_url\":\"http:\\/\\/pic.shicaiquanzi.com/upload/logo.jpg\"");
                    }

                    else
                    {
                        if (imglogo.Contains("shicaiquanzi"))
                        {
                            if (imglogo.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + imglogo + "\""); }
                            else { strTxt.Append(",\"img_url\":\"http://" + imglogo + "\""); }
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + imglogo + "\"");
                        }
                        //strTxt.Append(",\"img_url\":\"http://www.shicaiquanzi.com" + imglogo + "\"");
                    }
                    string _pro_remark = dr["pro_remark"].ToString();
                    if (_pro_remark.Length > 32) { _pro_remark = _pro_remark.Substring(0,32)+"..."; }
                    strTxt.Append(",\"pro_remark\":\"" + Microsoft.JScript.GlobalObject.escape(_pro_remark) + "\"");
                    strTxt.Append(",\"group_id\":" + dr["group_id"]);

                    #region 
                    BLL.product bll1 = new BLL.product();
                    DataSet ds1 = bll1.GetList(3, "status=1 and company_id=" + dr["id"], "add_time desc");
                    strTxt.Append(",\"pronum\":" + ds1.Tables[0].Rows.Count);
                    if (ds1.Tables[0].Rows.Count > 0)
                    {
                        strTxt.Append(",\"product_list\":[");
                        for (int j = 0; j < ds1.Tables[0].Rows.Count; j++)
                        {
                            DataRow drpro = ds1.Tables[0].Rows[j];
                            strTxt.Append("{");
                            strTxt.Append("\"id\":" + drpro["id"]);
                            strTxt.Append(",\"title\":\"" + drpro["title"] + "\"");
                            strTxt.Append(",\"market_price\":\"" + drpro["price"] + "\"");
                            strTxt.Append(",\"unit\":\"" + drpro["unit"] + "\"");
                            string proimg = drpro["img_url"].ToString();

                            if (proimg.Contains("shicaiquanzi"))
                            {
                                if (proimg.Contains("http://")) { strTxt.Append(",\"img_url\":\"" + proimg + "\""); }
                                else { strTxt.Append(",\"img_url\":\"http://" + proimg + "\""); }
                            }
                            else
                            {
                                strTxt.Append(",\"img_url\":\"http://pic.shicaiquanzi.com" + proimg + "\"");
                            }
                            strTxt.Append("}");

                            //是否加逗号
                            if (j < ds1.Tables[0].Rows.Count - 1)
                            {
                                strTxt.Append(",");
                            }
                        }

                        strTxt.Append("]");
                    }
                    else { strTxt.Append(",\"product_list\":[]"); }
                    #endregion


                    //结束
                    strTxt.Append("}");
                    //是否加逗号
                    if (i < ds.Tables[0].Rows.Count - 1)
                    {
                        strTxt.Append(",");
                    }
                }
                strTxt.Append("],");
            }
            else
            {
                strTxt.Append("\"company_list\":[],");
            }
            strTxt.Append("\"result\":" + ds.Tables[0].Rows.Count.ToString() + "");
            strTxt.Append("}");

            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}